# -*- coding:utf-8 -*-
# coreseek3.2 python connect mongodb
# author: Bruce
# date: 2013-02-25

from os import path
import os
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import pymongo
import datetime
import pprint
import codecs

pp = pprint.PrettyPrinter()

class MainSource(object):
    def __init__(self, conf):
        self.conf =  conf
        self.idx = 0
        self.data = []
        self.conn = None
        self.db = None

    def GetScheme(self):  #获取结构，docid、文本、整数
        return [
            ('id' , {'docid':True, } ),
            ('words', { 'type':'text'} ),
        ]

    def GetFieldOrder(self): #字段的优先顺序
        return [('words')]
        
    def Connected(self):   #如果是数据库，则在此处做数据库连接
        if self.conn==None:       
            autocomplete_words_object = codecs.open("/root/coreseek-4.1-beta/testpack/etc/pysource/csft_pymongo/data/autocomplete_keywords_list.txt","r","utf-8")
            for i in autocomplete_words_object:
                self.data.append(i)
        pass

    def NextDocument(self,err=''):   #取得每一个文档记录的调用
        if self.idx < len(self.data):
            self.docid = self.id = self.idx + 1 #'docid':True
            self.words = self.data[self.idx]
            self.idx += 1
            return True
        else:
            return False

if __name__ == "__main__":    #直接访问演示部分
    conf = {}
    source = MainSource(conf)
    source.Connected()

    while source.NextDocument():
        print "id=%d, words=%s" % (source.id, source.words)
    pass
#eof
